<template>
  <div class="minimal-device">
    <div class="device-card">
      <h2 class="device-title">添加设备</h2>
      <el-form 
        ref="deviceFormRef"
        :model="deviceForm"
        :rules="rules"
        class="device-form"
        label-width="100px"
      >
        <el-form-item label="设备名称" prop="name">
          <el-input
            v-model="deviceForm.name"
            placeholder="请输入设备名称"
            prefix-icon="Monitor"
            size="large"
          />
        </el-form-item>
        
        <el-form-item label="设备类型" prop="type">
          <el-input
            v-model="deviceForm.type"
            placeholder="请输入设备类型"
            prefix-icon="Cpu"
            size="large"
          />
        </el-form-item>
        
        <el-form-item label="用户数量" prop="userCount">
          <el-input-number
            v-model="deviceForm.userCount"
            :min="0"
            size="large"
            style="width: 100%"
          />
        </el-form-item>
        
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="deviceForm.createTime"
            type="date"
            placeholder="选择日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            size="large"
            style="width: 100%"
          />
        </el-form-item>
        
        <el-form-item label="设备描述" prop="description">
          <el-input
            type="textarea"
            :rows="3"
            v-model="deviceForm.description"
            placeholder="请输入设备描述"
            size="large"
          />
        </el-form-item>
        
        <el-button
          type="primary"
          size="large"
          class="device-btn"
          @click="submitForm"
          :loading="loading"
        >
          添加设备
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const deviceForm = reactive({
  name: '',
  type: '',
  userCount: 0,
  createTime: '',
  description: ''
})

const rules = reactive({
  name: [
    { required: true, message: '请输入设备名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请输入设备类型', trigger: 'blur' }
  ],
  userCount: [
    { required: true, message: '请输入用户数量', trigger: 'blur' }
  ],
  createTime: [
    { required: true, message: '请选择创建时间', trigger: 'change' }
  ]
})

const loading = ref(false)
const deviceFormRef = ref(null)

const submitForm = () => {
  deviceFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const response = await axios.post('/api/device/insertDevice', deviceForm)

        if (response.data.code === 1) {
          ElMessage.success('设备添加成功')
          deviceFormRef.value.resetFields()
        } else if (response.data.code === -1) {
          ElMessage.warning('该设备已存在，无法重复添加')
        } else {
          ElMessage.error('设备添加失败')
        }
      } catch (error) {
        console.error('添加设备出错:', error)
        ElMessage.error('添加设备时发生错误')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.minimal-device {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.device-card {
  width: 500px;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  max-width: 100%;
  overflow: auto;
}

.device-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 20px;
  font-weight: normal;
}

.device-form :deep(.el-form-item) {
  margin-bottom: 20px;
}

.device-btn {
  width: 100%;
  margin-top: 10px;
}
</style>